﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CalcCalorie
{
    public partial class FormFind : Form
    {
        public List<Point> fl = new List<Point>();
        public bool bOk = false;
        public DIR DB = null;

        public int ActT { get { return fl[lb.SelectedIndex].X; } }
        public int ActD { get { return fl[lb.SelectedIndex].Y; } }
        
        public FormFind()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormFind_Shown(object sender, EventArgs e)
        {
            fl.Clear();
            lb.Items.Clear();
            txb.Text = "";
            bOk = false;
            txb.Focus();
        }

        bool FindLike(String sourceStr, String likeStr)
        {
            if (sourceStr == "") return false;
            if (likeStr == "") return false;

            String s = sourceStr.ToUpper();
            String s1 = likeStr.ToUpper();
            int n = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == s1[n])
                {
                    n++;
                    if (n == s1.Length) return true;
                }
                else n = 0;
            }
            
            return false;
        }

        private void txb_TextChanged(object sender, EventArgs e)
        {
            fl.Clear();
            lb.Items.Clear();
            string str = txb.Text;
            if (str == "") return;

            for (int t = 0; t < DB.Items.Count; t++)
            {
                for (int d = 0; d < DB.Items[t].Items.Count; d++)
                {
                    String s = DB.Items[t].Items[d].Name;
                    if (FindLike(s, str))
                    {
                        fl.Add(new Point(t, d));
                        lb.Items.Add(DB.Items[t].Name + " >> " + s);
                    }
                }
            }
        }

        private void lb_DoubleClick(object sender, EventArgs e)
        {
            if (lb.SelectedIndex == -1) return;
            bOk = true;
            Close();
        }

        private void txb_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) { e.IsInputKey = false; Close(); }
            if (e.KeyCode == Keys.Enter && lb.Items.Count == 1)
            {
                e.IsInputKey = false;
                bOk = true;
                lb.SelectedIndex = 0;
                Close();
            }
        }

        private void lb_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) { e.IsInputKey = false; Close(); }
            if (e.KeyCode == Keys.Enter) { e.IsInputKey = false; lb_DoubleClick(null, null); }
        }

    }
}
